package com.adams.custom.page.core.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.adams.access.db.bean.DicBusinessDealFlow;
import com.adams.access.db.dao.hibernate.HibernateGenericDao;
import com.adams.custom.page.util.ViewCacheUtil;
import com.adams.custom.page.util.ViewUtil;

/**
 * 
 * File Name : DicBusinessDealFlowDaoImpl.java
 * 
 * @Description : 业务处理流定义数据访问实现类
 */
public class DicBusinessDealFlowDaoImpl extends
		HibernateGenericDao<DicBusinessDealFlow, Long>
{

	private static String cacheMapByPk_key = "DicBusinessDealFlow_cacheMapByPk";

	private static String cacheMapByTablecodeAndActionType_key = "DicBusinessDealFlow_cacheMapByTablecodeAndActionType";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.adams.access.db.dao.GenericDao#get(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public DicBusinessDealFlow get(Serializable id)
	{
		String key = id + "";
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		if (!cacheMapByPk.containsKey(key))
		{
			synchronizationCache();
		}
		return (DicBusinessDealFlow) cacheMapByPk.get(key);
	}

	/**
	 * Description : 根据动作类型及表编码 得到DicBusinessDealFlow列表
	 * 
	 * @param viewid
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<DicBusinessDealFlow> findByActionTypeAndTablecode(
			String actionType, String tablecode)
	{
		String key = actionType + "|" + tablecode;
		Map cacheMapByTablecodeAndActionType = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByTablecodeAndActionType_key);
		if (!cacheMapByTablecodeAndActionType.containsKey(key))
		{
			synchronizationCache();
		}
		if (!cacheMapByTablecodeAndActionType.containsKey(key))
		{
			key = actionType + "|";
		}
		List<DicBusinessDealFlow> result = new ArrayList<DicBusinessDealFlow>();
		List<DicBusinessDealFlow> temp = (List<DicBusinessDealFlow>) cacheMapByTablecodeAndActionType
				.get(key);
		if (null != temp)
		{
			result.addAll(temp);
		}
		return result;
	}

	/**
	 * Description : 根据动作类型 得到默认DicBusinessDealFlow处理列表
	 * 
	 * @param viewid
	 * @return
	 * 
	 */
	public List<DicBusinessDealFlow> findDefaultByActionType(String actionType)
	{
		return findByActionTypeAndTablecode(actionType, "");
	}

	private void sort(List<DicBusinessDealFlow> result)
	{
		if (null == result || result.size() == 0)
		{
			return;
		}
		Collections.sort(result, new Comparator<DicBusinessDealFlow>()
		{
			public int compare(DicBusinessDealFlow o1, DicBusinessDealFlow o2)
			{
				if (o1.getOrderid() > o2.getOrderid())
				{
					return 1;
				} else if (o1.getOrderid() == o2.getOrderid())
				{
					if (o1.getId() > o2.getId())
					{
						return 1;
					} else if (o1.getId() == o2.getId())
					{
						return 0;
					}
					return -1;
				}
				return -1;
			}
		});
	}

	private List<DicBusinessDealFlow> findAllFromDB()
	{
		List<DicBusinessDealFlow> result = query("from DicBusinessDealFlow");
		return result;
	}

	/**
	 * Description : 同步缓存
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<DicBusinessDealFlow> synchronizationCache()
	{
		List<DicBusinessDealFlow> result = findAllFromDB();
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		Map cacheMapByTablecodeAndActionType = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByTablecodeAndActionType_key);
		cacheMapByTablecodeAndActionType.clear();
		for (DicBusinessDealFlow dicBusinessDealFlow : result)
		{
			cacheMapByPk.put(dicBusinessDealFlow.getId() + "",
					dicBusinessDealFlow);

			String actionType = dicBusinessDealFlow.getActionType();
			String tablecode = dicBusinessDealFlow.getTablecode();
			String key = actionType + "|";
			if (!ViewUtil.isNuLLStr(tablecode))
			{
				key += tablecode;
			}
			if (!"".equals(key))
			{
				List<DicBusinessDealFlow> list = null;
				if (cacheMapByTablecodeAndActionType.containsKey(key))
				{
					list = (List<DicBusinessDealFlow>) cacheMapByTablecodeAndActionType
							.get(key);
				} else
				{
					list = new ArrayList<DicBusinessDealFlow>();
				}
				list.add(dicBusinessDealFlow);
				cacheMapByTablecodeAndActionType.put(key, list);
			}
		}

		// 排序处理
		Set<String> keys = cacheMapByTablecodeAndActionType.keySet();
		for (String key : keys)
		{
			List<DicBusinessDealFlow> dicBusinessDealFlows = (List<DicBusinessDealFlow>) cacheMapByTablecodeAndActionType
					.get(key);
			sort(dicBusinessDealFlows);
			cacheMapByTablecodeAndActionType.put(key, dicBusinessDealFlows);
		}

		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByPk_key, cacheMapByPk);
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByTablecodeAndActionType_key,
				cacheMapByTablecodeAndActionType);
		return result;
	}
}
